﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UBlockly;
using UBlockly.Scriptable;
using UBlockly.Compotent;
using UBlockly.Graphic;
using UBlockly.Sys;
using UBlockly.UI;
using UnityEditor;
using UnityEngine.UI;
using Object = UnityEngine.Object;

public class TestLoad : MonoBehaviour, IBlockLoader
{
    [SerializeField] GameObject blockApi;
    [SerializeField] UBlocklyPhysic ublocklyGo;
    [SerializeField] bool loadFromFile;

    [SerializeField] private Texture targetTex;

    private bool isBlockGraphicInit = false;

    private UBlockEntry blockEntry;

    void Awake()
    {
        BlockFramework.InitFramework(this);
        isBlockGraphicInit = true;
        blockEntry = BlockFramework.CreateBlockEntry(ublocklyGo);
        blockEntry.ExtralHandler.NodeClickHandler = (hashdd, data, o, arg3) =>
        {
            Debug.Log("执行链");
//            blockEntry.RunNode(o.Node.Value, true);
//            var treeNode = SerializeTreeNode.SerializeNode(o.Node.Value);
//            if (treeNode != null)
//            {
//                var str = treeNode.ToString();
//                Debug.LogError("shu:" + str);
//            }
//            else
//            {
//                ComplexNode cNode =(ComplexNode) o.Node.Value;
//                if(cNode != null)
//                    cNode.DisplayDetails();
//            }

            ComplexNode cNode =(ComplexNode) o.Node.Value;
            if(cNode != null)
                cNode.DisplayDetails();
        };

    }



    public void TestFun(int a)
    {
    }

    void Start()
    {
//        Debug.LogError("testType:"+this.GetType().FullName);
//        var type = System.Type.GetType("TestLoad.TestFun");
//        var targetType = this.GetType().GetMethod("TestFun").CreateDelegate();
//        Debug.LogError("tttgggtt:"+targetType);
//        var assetName = "Assets/BlockRes/BlockGraphic.asset";
//        var obj = AssetDatabase.LoadAssetAtPath<BlockGraphicAsset>(assetName);
//        Debug.Log(obj.name);
    }

    private void Update()
    {
        if (!isBlockGraphicInit)
            return;
        if (Input.GetKeyUp(KeyCode.A))
        {
            ApiModeTest();
        }

        if (Input.GetKeyUp(KeyCode.B))
        {
            StrModeTest();
        }

        if (Input.GetKeyUp((KeyCode.C)))
        {
            //ComplexModeTest1();
            ComplexModeTest2();
        }
    }


    void ApiModeTest()
    {
        var api = blockApi.GetComponent<IBlockApi>();
        blockEntry.RegesitBlockApi(api);
        blockEntry.InitSiderBar(new ClassiflyItem[]
                {new ClassiflyItem("测试1"), new ClassiflyItem("测试2"), new ClassiflyItem("测试3")}
            , api);
    }

    void StrModeTest()
    {
        blockEntry.CreateSpaceNode("VoidR Testee0(IntR x,IntR y)", new string[3] {"让", "", "+    "});
        blockEntry.CreateSpaceNode("VoidR Testee1(IntR x,IntR y)", new string[3] {"让", "", "+    "});
    }

    void ComplexModeTest1()
    {
        var str = new string[] { "1-Walk#1-jjj#0-GetIntR@IntR&55#","1-WaitTime#0-JiaYou#","1-Walk#0-GetIntR@IntR&0#","0-HideSelf#"};
        blockEntry.CreateSpaceNode("VoidR TestComplex()", str, null);
    }
    void ComplexModeTest2()
    {
        var str = new string[] { "2-TestIntR#1-jjj#0-JiaYou#0-JiaYou#"};
        blockEntry.CreateSpaceNode("IntR1 TestComplex()", str, "ComplexNode");
    }


public string CustomConfigPath => "Assets/BlockRes";
    public void LoadResource<T>(string assetName, System.Action<T> callback) where T : Object
    {
     //   assetName = assetName.Substring(0, assetName.LastIndexOf('.'));
        Debug.LogError("fileName = "+assetName);
        var asset = AssetDatabase.LoadAssetAtPath<T>(assetName);
        callback?.Invoke(asset);
    }
}

public class BImage : Image
{
    
    
}

